/*******************************************************************************
 * Copyright 2012 Chao Zhang
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

package org.jbiolib.io;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.Reader;
import java.util.ArrayList;

/**
 * @author Chao Zhang
 * @version 1.0 2012-11-06
 */

public class FileRead {
	
	public static String readFile(String fileName, String type) {
		String dataArray = null;
		try {
			BufferedReader in = new BufferedReader(new FileReader(fileName));
			int i = 0;
			while(in.readLine() != null){
				i++;
			}
			dataArray = "";
			in = new BufferedReader(new FileReader(fileName));
			String line = "";
			i = 0;
			do {
				line = in.readLine();
				if(line != null) {
					dataArray = dataArray + "\n" + line;
				}
				i++;
			} while(line != null);
			in.close();
		} catch (Exception e) {
			dataArray = null;
			e.printStackTrace();
		}
		return dataArray;
	}

    public static ArrayList readFile(String fileName) {
		ArrayList dataArray = new ArrayList();
		try {
			Reader fReader = new FileReader(fileName);
			BufferedReader in = new BufferedReader(fReader);
			String line = "";
			do {
				line = in.readLine();
				if(line != null){
					dataArray.add(line);
				}
			} while(line != null);
			in.close();
			fReader.close();
		} catch (Exception e) {
			dataArray = null;
			e.printStackTrace();
		}
		return dataArray;
	}
	
	public static String[] readFile(String fileName, String type[]) {
		String[] dataArray = null;
		try {
			Reader fReader = new FileReader(fileName);
			BufferedReader in = new BufferedReader(fReader);
			int i = 0;
			while(in.readLine() != null){
				i++;
			}
			in.close();
			fReader.close();
			
			dataArray = new String[i];
			fReader = new FileReader(fileName);
			in = new BufferedReader(fReader);
			i = 0;
			String line = "";
			do {
				line = in.readLine();
				if(line != null){
					dataArray[i] = line;
				}
				i++;
			} while(line != null);
			in.close();
			fReader.close();
		} catch (Exception e) {
			dataArray = null;
			e.printStackTrace();
		}
		return dataArray;
	}

	public static String[][] readStringArray(String fileName, int row) {
		String[][] dataArray;// = new double[row][column];
		try {
			BufferedReader in = new BufferedReader(new FileReader(fileName));
            String line = in.readLine();
            int column = line.split("\t").length;
            dataArray = new String[row][column];
            in = new BufferedReader(new FileReader(fileName));
            line = "";
            int i = 0;
			do {
				line = in.readLine();
				if(line != null){
					String[] temp = line.split("\t");
					for(int j=0; j<column; j++) {
						dataArray[i][j] = temp[j];
					}
				}
				i++;
			} while((line != null)&&(i<row));
			in.close();
		} catch (Exception e) {
			dataArray = null;
			e.printStackTrace();
		}
		return dataArray;
	}

	public static double[][] readDoubleArray(String fileName, int row, int column) {
		double[][] dataArray = new double[row][column];
		try {
			BufferedReader in = new BufferedReader(new FileReader(fileName));
			int i = 0;
			String line = "";
			do {
				line = in.readLine();
				if(line != null){
					String[] temp = line.split("\t");
					for(int j=0; j<column; j++) {
						dataArray[i][j] = new Double(temp[j]).doubleValue();
					}
				}
				i++;
			} while((line != null)&&(i<row));
			in.close();
		} catch (Exception e) {
			dataArray = null;
			e.printStackTrace();
		}
		return dataArray;
	}

	public static double[] readDoubleArray(String fileName, int row) {
		double[] dataArray = new double[row];
		try {
			BufferedReader in = new BufferedReader(new FileReader(fileName));
			int i = 0;
			String line = "";
			do {
				line = in.readLine();
				if(line != null){
					dataArray[i] = new Double(line.trim()).doubleValue();
				}
				i++;
			} while((line != null)&&(i<row));
			in.close();
		} catch (Exception e) {
			dataArray = null;
			e.printStackTrace();
		}
		return dataArray;
	}
}

